import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:family_app/src/features/finance/widgets/custom_chart.dart';

/// 统计分析屏幕
/// 显示收支趋势、分类统计等图表
class StatsScreen extends ConsumerWidget {
  const StatsScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return SingleChildScrollView(
      child: Padding(
        padding: EdgeInsets.all(16.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
              // 收支趋势折线图
              Container(
                padding: EdgeInsets.all(16.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12.r),
                  border: Border.all(color: Colors.grey[200]!),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.05),
                      blurRadius: 4,
                      offset: const Offset(0, 2),
                    ),
                  ],
                ),
                child: TrendChart(
                  title: '收支趋势（本周）',
                  expenseData: [100, 150, 120, 200, 180, 220, 190],
                  incomeData: [500, 500, 500, 500, 500, 500, 500],
                ),
              ),
              SizedBox(height: 20.h),

              // 分类统计饼图
              Container(
                padding: EdgeInsets.all(16.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12.r),
                  border: Border.all(color: Colors.grey[200]!),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.05),
                      blurRadius: 4,
                      offset: const Offset(0, 2),
                    ),
                  ],
                ),
                child: CategoryPieChart(
                  title: '分类统计（本月）',
                  categoryData: {
                    '餐饮': 500,
                    '购物': 800,
                    '交通': 300,
                    '娱乐': 400,
                    '其他': 200,
                  },
                ),
              ),
              SizedBox(height: 20.h),

              // 年度对比柱状图
              Container(
                padding: EdgeInsets.all(16.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12.r),
                  border: Border.all(color: Colors.grey[200]!),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.05),
                      blurRadius: 4,
                      offset: const Offset(0, 2),
                    ),
                  ],
                ),
                child: BarChartWidget(
                  title: '年度支出对比',
                  data: [3200, 3500, 3100, 4200, 3800, 4100, 3900, 4300, 3600, 3900, 4200, 3800],
                ),
              ),
              SizedBox(height: 20.h),

              // 统计摘要
              _buildStatsSummary(),
            ],
          ),
        ),
    );
  }

  /// 构建统计摘要
  Widget _buildStatsSummary() {
    return Container(
      padding: EdgeInsets.all(16.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(color: Colors.grey[200]!),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.05),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '本月统计',
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
              color: Colors.black87,
            ),
          ),
          SizedBox(height: 16.h),
          _buildSummaryRow('总收入', '¥8,500.00', Colors.green),
          SizedBox(height: 12.h),
          _buildSummaryRow('总支出', '¥3,200.00', Colors.red),
          SizedBox(height: 12.h),
          _buildSummaryRow('结余', '¥5,300.00', Colors.blue),
          SizedBox(height: 12.h),
          _buildSummaryRow('平均日支出', '¥106.67', Colors.orange),
        ],
      ),
    );
  }

  /// 构建摘要行
  Widget _buildSummaryRow(String label, String value, Color color) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: 13.sp,
            color: Colors.grey[600],
          ),
        ),
        Text(
          value,
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w600,
            color: color,
          ),
        ),
      ],
    );
  }
}
